package com.dudu.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.bean.AccountBean;
import com.dudu.bean.CommunicationDataQuerying;
import com.dudu.service.IAccountExtendService;
import com.dudu.service.ICheckValidityService;
import com.dudu.service.ICommunicationDataService;
import com.dudu.service.impl.AccountExtendServiceImpl;
import com.dudu.service.impl.CheckValidityServiceImpl;
import com.dudu.service.impl.CommunicationDataImpl;
import core.CoreService;

/**
 * 
 * @author jack
 * 
 */
public class CommunicationDataController extends CoreService implements ICommunicationDataController {

	public Logger logger = Logger.getLogger(CommunicationDataController.class);
	private ICommunicationDataService communicationDataService = new CommunicationDataImpl();
	private IAccountExtendService accountExtendService = new AccountExtendServiceImpl();
	private ICheckValidityService checkValidityService = new CheckValidityServiceImpl();

	@Override
	public String queryData() {
		// 接口请求参数
		List<String[]> checkParamlist = new ArrayList<String[]>();
		// app_key应用标识
		String app_key = this.getRequest().getParameter("app_key");
		String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
		checkParamlist.add(app_keyStr);
		// cust_account账号标识
		String cust_account = this.getRequest().getParameter("cust_account");
		String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
		checkParamlist.add(cust_accountStr);
		// 时间戳
		String timestamp = this.getRequest().getParameter("timestamp");
		String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
		checkParamlist.add(timestampStr);
		// 认证码
		String sign = this.getRequest().getParameter("sign");
		String[] signStr = { "sign", sign, "50", "0", "str" };
		checkParamlist.add(signStr);
		String productKey = this.getRequest().getParameter("product_key");
		JSONObject object = null;
		try {
			object = this.checkParams(checkParamlist);
			if (null == object) {
				object = new JSONObject();
				// 根据cust_account查询账号信息
				AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
				// 判断账号信息是否找到
				if (accountBean != null) {
					// 验证秘钥信息
					if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {

						CommunicationDataQuerying bean = new CommunicationDataQuerying();
						bean.setApp_key(app_key);
						bean.setCust_account(cust_account);
						if (StringUtils.isNotBlank(productKey)) {
							bean.setProduct_key(productKey);
						}
						String serialNumber = UUID.randomUUID().toString();
						List<JSONObject> data = this.communicationDataService.queryCommunicationDataQueryingList(bean, serialNumber);
						if(data.size() > 0){
							object.put("result", "0");
							object.put("describe", "success");
							object.put("code", "0000");
							object.put("data", data.toString());
							object.put("serial_number", serialNumber);
						}else{
							object.put("result", "-1");
							object.put("describe", "查询结果为空");
							object.put("code", "0705");
						}
					} else {
						object.put("result", "-1");
						object.put("describe", "参数sign错误");
						object.put("code", "0001");
					}
				} else {
					object.put("result", "-1");
					object.put("describe", "计费账号不存在");
					object.put("code", "0002");
				}
			} else {
				//
			}
		} catch (Exception e) {
			object.put("result", "-1");
			object.put("describe", "查询异常");
			object.put("code", "0701");
		}
		return returnData(object.toString());
	}

	@Override
	public String successConfirm() {
		// 接口请求参数
				List<String[]> checkParamlist = new ArrayList<String[]>();
				// app_key应用标识
				String app_key = this.getRequest().getParameter("app_key");
				String[] app_keyStr = { "app_key", app_key, "50", "0", "str" };
				checkParamlist.add(app_keyStr);
				// cust_account账号标识
				String cust_account = this.getRequest().getParameter("cust_account");
				String[] cust_accountStr = { "cust_account", cust_account, "50", "0", "str" };
				checkParamlist.add(cust_accountStr);
				// 时间戳
				String timestamp = this.getRequest().getParameter("timestamp");
				String[] timestampStr = { "timestamp", timestamp, "20", "0", "str" };
				checkParamlist.add(timestampStr);
				// 认证码
				String sign = this.getRequest().getParameter("sign");
				String[] signStr = { "sign", sign, "50", "0", "str" };
				checkParamlist.add(signStr);
				//流水号
				String serialNumber = this.getRequest().getParameter("serial_number");
				String[] serialNumberStr = { "serial_number", serialNumber, "50", "0", "str" };
				checkParamlist.add(serialNumberStr);
				JSONObject object = null;
				try {
					object = this.checkParams(checkParamlist);
					if (null == object) {
						object = new JSONObject();
						// 根据cust_account查询账号信息
						AccountBean accountBean = accountExtendService.queryAccountDetailByCustAccount(cust_account);
						// 判断账号信息是否找到
						if (accountBean != null) {
							// 验证秘钥信息
							if (checkValidityService.toolsCheckSign(cust_account, accountBean.getKey_private(), timestamp, sign)) {
								int ret = this.communicationDataService.deleteCommunicationDataQueryingBySerialNumber(serialNumber);
								if(ret > 0){
									object.put("result", "0");
									object.put("describe", "success");
									object.put("code", "0000");
								}else{
									object.put("result", "-1");
									object.put("describe", "error");
									object.put("code", "0710");
								}
							} else {
								object.put("result", "-1");
								object.put("describe", "参数sign错误");
								object.put("code", "0001");
							}
						} else {
							object.put("result", "-1");
							object.put("describe", "计费账号不存在");
							object.put("code", "0002");
						}
					} else {
						//
					}
				} catch (Exception e) {
					object.put("result", "-1");
					object.put("describe", "接口数据解析异常");
					object.put("code", "0702");
				}
				return returnData(object.toString());
	}

}
